﻿using System;
using System.Collections.Generic;
using System.Text;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.Redis;
using Brc_PaaS.Dal;

namespace Brc_PaaS.Bll.Redis
{
    public class Redis : IRedis
    {
        private IRepository<Ass_Redis> _ass_redis;
        public Redis(IRepository<Ass_Redis> ass_redis)
        {
            _ass_redis = ass_redis;
        }
        public ResponseModel<Ass_Redis> AutoDistributionDb(string systemId)
        {
            ResponseModel<Ass_Redis> result = new ResponseModel<Ass_Redis>() { IsSuccess = false, Msg = string.Empty, Data = new Ass_Redis() };
            var isExist = _ass_redis.Exist(i => i.SystemId == systemId);
            if(isExist)
            {
                result.Msg = "您已存在一个缓存数据库，不能再申请分配新库，如需帮助，请联系管理员";
                return result;
            }
            var dbRedis = _ass_redis.Get(i => i.Status == 1 && string.IsNullOrEmpty(i.SystemId));
            if (dbRedis == null)
            {
                result.Msg = "暂无可分配的缓存数据库，请联系管理员";
                return result;
            }
            dbRedis.SystemId = systemId;
            result.IsSuccess = _ass_redis.Update(dbRedis) > 0;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败，请联系管理员";
            result.Data = dbRedis;
            return result;
        }


        public BaseResponseModel DisabledDb(string id)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            var dbRedis = _ass_redis.Get(i => i.Status == 1 && i.Id == id);
            if (dbRedis == null)
            {
                result.Msg = "未能获取到对应的数据库信息，请联系管理员";
                return result;
            }
            dbRedis.Status = 0;
            result.IsSuccess = _ass_redis.Update(dbRedis) > 0;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败，请联系管理员";
            return result;
        }

        public ResponseModel<Ass_Redis> GetRedisDb(string systemId)
        {
            ResponseModel<Ass_Redis> result = new ResponseModel<Ass_Redis>() { IsSuccess = false, Msg = string.Empty, Data = new Ass_Redis() };
            var dbRedis = _ass_redis.Get(i => i.Status == 1 && i.SystemId == systemId);
            if (dbRedis == null)
            {
                result.Msg = "未能获取到对应数据库信息";
                return result;
            }
            result.IsSuccess = true;
            result.Data = dbRedis;
            return result;
        }

        public BaseResponseModel InitRedisDb(RedisInitRequestModel model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            var dbCount = 16;
            var i = model.IsExcludeZero ? 1 : 0;
            var preKey = model.Ip.Replace(".", "");
            var curDateTime = DateTime.Now;
            var list = new List<Ass_Redis>();
            do
            {
                var id = preKey + model.Port + i;
                Ass_Redis redis = new Ass_Redis()
                {
                    Id = id,
                    Ip = model.Ip,
                    Port = model.Port,
                    DbNumber = i,
                    Status = 1,
                    Passwords = model.PassWord,
                    CreateTime = curDateTime,
                    CreateUserId = model.CreateUserId
                };
                list.Add(redis);
                i++;
            } while (i < dbCount);
            result.IsSuccess = _ass_redis.AddList(list) > 0;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败，请联系管理员";
            return result;
        }
    }
}
